<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\facade\Config;
use app\index\controller\User;
use think\facade\Session;
class Appquality extends User
{
    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");
        if(!$this->token){
        $this->redirect("index/user/login");
        }
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($name="")
    {
        Session::delete('name');
        $data = json_decode($this->curl(),true);
        
        if($data['code']==401){
            $this->redirect("index/user/login");
        }
        if($data['code']==403||$data['code']==0){
            $this->error($data['msg']);
        }
            $data=$data['data'];
            $typeNames=[];
            $tableList=[];
            if($data!=[]){
                foreach ($data as $info){
                    $typeNames[]=$info['name'];
                }
            }
            
            $list=[];
            return view("index",['typeNames'=>$typeNames,"list"=>$list]);
    }
    public function getNames($name=""){
        Session::delete('name');
        $name = $this->request->request('name');
        $data = json_decode($this->curl(),true);
        if($data['code']==401){
            $this->redirect("index/user/login");
        }
        $data=$data['data'];
        if($data!=[]){
            foreach ($data as $info){
                $result[$info['name']]=$info['list'];
            }
        }
        if(empty($name)){
            $list['list']=[];
        }
        if(!empty($name)){
            $list['list']=$result[$name];
            return json($list);
        }
    }

    public function analysis()
    {
            $appName = $this->request->request('name');
            $type = $this->request->request('type');
            Session::set('name.appName',$appName);
            Session::set('name.type',$type);
            if(empty($appName)&&empty($type)){
                $this->redirect("index");
            }
            return view("analysis");
    }
    /**
     * delay 延时
     */
    public function delay(){
        $res = $this->getLink();
        // $res['code'] = 401;
        $arr['code'] = $res['code'];
        $arr['msg'] = $res['msg'];
        $arr['xAxis'] = [];
        $arr['series'] = [];
        $list=[];
        if($res['code']==401){
            $this->redirect("index/user/login");
        }
        if($res['code'] == 1&&$res['data']!=[]){
            $data = $res['data']['netWorkDelay'];
            foreach($data as $v){
                $arr['xAxis'][]=$v['timeInterval'];
                $list[]=$v['value'];
            }
            }
        $arr['series'][0]=['name'=>"时间",'data'=>$list];
        die(json_encode($arr,JSON_UNESCAPED_UNICODE));

    }
    /**
     * shaking 抖动
     */
    public function shaking(){
        $res = $this->getLink();
        // $res['code'] = 401;
        $arr['code'] = $res['code'];
        $arr['msg'] = $res['msg'];
        $arr['xAxis'] = [];
        $arr['series'] = [];
        $list=[];
        if($res['code']==401){
            $this->redirect("index/user/login");
        }
        if($res['code'] == 1&&$res['data']!=[]){
            $data = $res['data']['shaking'];
            foreach($data as $v){
                $arr['xAxis'][]=$v['timeInterval'];
                $list[]=$v['value'];
            }
        }

        $arr['series'][0]=['name'=>"时间",'data'=>$list];
        die(json_encode($arr,JSON_UNESCAPED_UNICODE));

    }
    /**
     * shaking 丢包率
     */
    public function packageLoss(){
        $res = $this->getLink();
        // $res['code'] = 401;
        $arr['code'] = $res['code'];
        $arr['msg'] = $res['msg'];
        $arr['xAxis'] = [];
        $arr['series'] = [];
        $list=[];
        if($res['code']==401){
            $this->redirect("index/user/login");
        }
        if($res['code'] == 1&&$res['data']!=[]){
            $data = $res['data']['packageLoss'];
            foreach($data as $v){
                $arr['xAxis'][]=$v['timeInterval'];
                $list[]=$v['value'];
            }
        }
        $arr['series'][0]=['name'=>"时间",'data'=>$list];
        die(json_encode($arr,JSON_UNESCAPED_UNICODE));

    }
    /**
     * 请求接口
     *获取所有应用类型
     * @return \think\Response
     */
    public function curl()
    {
        $data = array(
            'token' => session('login_token'),
            'terminal' => 'web'
        );
        $data = json_encode($data);
        $url = request()->domain() . '/api/quality/apps';
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $res = curl_post($url,$data,$curlopt);
        return $res;
    }
    /**
     * 请求接口
     *应用质量接口
     * @return \think\Response
     */
    public function getQuality()
    {
        $data = array(
            "propertyname"=> "basedlinkdetails",
            'token' => session('login_token'),
            'terminal' => 'web',
            'gradename' => '每天',
            'starttime' => date("Y-m-d 00:00:00",time()),
            'endtime' => date("Y-m-d H:i:s", time()),
        );
        $data_string = json_encode($data, JSON_UNESCAPED_UNICODE);
        // dump($data_string);die;
        $curlopt = array(
            CURLOPT_HTTPHEADER => array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)),
            CURLOPT_TIMEOUT => Config::get("site.api_timeout"),
        );
        $url = request()->domain() . '/api/nqm/link';
        $res = curl_post($url, $data_string, $curlopt);
        $res = json_decode($res, true);
        return $res;
    }
    /**
     * 请求接口
     *应用质量分析接口
     * @return \think\Response
     */
    public function getLink()
    {
        $data = array(
            "propertyname"=> "basedlinkdetails",
            'token' => session('login_token'),
            'terminal' => 'web',
            'gradename' => '5分钟',
            'starttime' => date("Y-m-d 00:00:00",time()),
            'endtime' => date("Y-m-d H:i:s", time()),
            "apptype"=>session('name.type'),
            "appname"=>session('name.appName')
        );
        $data_string = json_encode($data, JSON_UNESCAPED_UNICODE);
        // dump($data_string);die;
        $curlopt = array(
            CURLOPT_HTTPHEADER => array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)),
            CURLOPT_TIMEOUT => Config::get("site.api_timeout"),
        );
        $url = request()->domain() . '/api/nqm/link';
        $res = curl_post($url, $data_string, $curlopt);
        $res = json_decode($res, true);
        return $res;
    }


    //主要应用链接----------旧的
    public function appl()
    { 
        $datas = array(
            'token'         =>  session('login_token'),
            'terminal'      =>  'web',
            "propertyname"  =>  "basedlinkdetails_sort",
            "gradename"     =>  "每天",
            'starttime' => date("Y-m-d 00:00:00",time()),
            'endtime' => date("Y-m-d H:i:s", time()),
        );
        $datas = json_encode($datas);
        $link = request()->domain() . '/api/nqm/link';
        $curlopts = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $datas ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $ress = curl_post($link,$datas,$curlopts);
        return $ress;
    }
    



    //主要应用连接----------对接新版
    public function main_app()
    {
        $datas = array(
            'token'         =>  session('login_token'),
            'terminal'      =>  'web',
            "propertyname"  =>  "basedlinkdetails_sort",
            "gradename"     =>  "每天",
            "typenum"       =>  5,
            'starttime' => date("Y-m-d 00:00:00",time()),
            'endtime' => date("Y-m-d H:i:s", time()),
        );
        $datas = json_encode($datas);
        $link = request()->domain() . '/api/nqm/link';
        $curlopts = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $datas ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );
        $res = curl_post($link,$datas,$curlopts);
        return $res;
    }


}
